package org.icy.modules;

import org.ice.http.HttpRequest;
import org.ice.module.HttpModule;
import org.ice.view.JstlView;

public abstract class IcyModule extends HttpModule {
	
	protected String moduleUrl;
	
	public abstract String getModuleName();
	
	public void init() {
		HttpRequest hr = getRequest();
		hr.setAttribute("baseUrl", getBaseUrl());
		hr.setAttribute("resourceUrl", getResourceUrl());
		String query = hr.getQueryString();
		String queryString = (query == null || query.isEmpty())?"":("?"+query);
		hr.setAttribute("currentUrl", hr.getRequestURL()+queryString);
		setView(new JstlView());
		
		String cls = getModuleName();
		String subfolder = "/modules/mod_"+cls.toLowerCase();
		moduleUrl = "/resource"+subfolder;
		this.setTemplate(subfolder+"/"+getRequest().getTaskName()+".jsp");
	}
	
	public void setRequestAttribute(String name, Object value) {
		getRequest().setAttribute(name, value);
	}
	
	public void redirect(String controller, String task) {
		this.redirect(getBaseUrl()+"/"+controller+"/"+task);
	}
}
